﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Core.Persistent;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Core.MyLinq;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Domain.MainBoundedContext.SystemModule
{
    public class EmployeeSpecification : ISpecification<Employee, EmployeeSearch>
    {
        #region ISpecification<Employee,EmployeeSearch> Members

        public EmployeeSearch SearchParams
        {
            get;
            set;
        }

        #endregion

        #region ISpecification<Employee> Members

        public System.Linq.Expressions.Expression<Func<Employee, bool>> Expression
        {
            get 
            {
                IQueryBuilder<Employee> queryBuilder = QueryBuilder.Create<Employee>();

                if (null != SearchParams)
                {
                    if (SearchParams.ID.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(e => e.Id, SearchParams.ID);
                    }
                    if (SearchParams.UserAccountID > 0)
                    {
                        queryBuilder = queryBuilder.Equals(e => e.UserAccount.Id, SearchParams.UserAccountID);
                    }
                    if (SearchParams.HasUserAccount)
                    {
                        queryBuilder = queryBuilder.Greater(e => e.UserAccount.Id, 0);
                    }
                    if (SearchParams.DepartmentCode.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(e => e.Department.Id, SearchParams.DepartmentCode);
                    }
                    if (SearchParams.CorporationCode.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(e => e.Corporation.Id, SearchParams.CorporationCode);
                    }
                    queryBuilder = queryBuilder.Equals(e => e.IsDeleted, false);
                }

                return queryBuilder.Expression.Expand();
            }
        }

        #endregion
    }
}
